﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Bigger.Web.Mvc;
using Bigger.Framework;
using CJAtech.BBS.Web.Models;
using CJAtech.BBS.DataEntity;
using System.Web.Mvc;
using CJAtech.BBS.Process;
using Bigger.Web.Helper;

namespace CJAtech.BBS.Web.Filters
{
    [Obsolete("兼容旧版的SSO，新版本请使用SSOAuthFilter代替")]
    public class SSOAuthorFilter : BiggerActionFilter
    {
        /// <summary>
        /// 权限类型
        /// </summary>
        public PowerType PowerType { get; set; }
        public SSOAuthorFilter(PowerType powerType)
        {
            this.PowerType = powerType;
            this.Order = 0;
        }
        public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
        {

            if (this.PowerType == PowerType.None)
            {
                return;
            }

            var objCommunityConfig = filterContext.HttpContext.GetItems("CommunityConfig") as CommunityConfig;

            string token;
            if (SystemWebAsset.IsCrossDomain())//todo:Url有带取Url
            {
                token = filterContext.HttpContext.Request["Token"];

                //设置响应报头"Access-Control-Allow-Methods"
                //filterContext.HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
            }
            else
            {
                token = CookieHelper.GetCookieValue(objCommunityConfig.ConfigCode + "SSOToken");
            }

            SSOProcess objSSOProcess = new SSOProcess(objCommunityConfig);
            UserInfo objUserInfo = null;
            if (token.IsNoNull())
            {
                if (string.Equals(objCommunityConfig.ConfigCode, "benz", StringComparison.OrdinalIgnoreCase) || ConfigHelper.GetBoolValue("IsTest", false))
                {
                    objUserInfo = objSSOProcess.GetUserInfo(token);
                }
                else
                {
                    objUserInfo = objSSOProcess.GetUserInfoConvert(token);
                }
                if (objUserInfo != null)
                {
                    filterContext.Controller.ViewBag.UserInfo = objUserInfo;

                    return;
                }
                CookieHelper.RemoveCookie(objCommunityConfig.ConfigCode + "SSOToken");
                objSSOProcess.LoginOut(token);
            }


            if (SystemWebAsset.IsAjaxRequest() || SystemWebAsset.IsCrossDomain())
            {
                filterContext.Result = new JsonResult
                {
                    Data = new InvokeResult { ResultCode = "NotLogin", ResultMessage = "对不起，请先登录" },
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
                return;
            }
            string requestUrl = filterContext.HttpContext.Request.Url.AbsoluteUri;
            //LogHelper.WriteLog("请求地址", requestUrl);
            filterContext.Result = new RedirectResult(objSSOProcess.CreateRedirectUrl(requestUrl));
        }
    }

    /// <summary>
    /// 权限校验
    /// </summary>
    public enum PowerType
    {
        /// <summary>
        /// 无需任何权限校验
        /// </summary>
        None,
        /// <summary>
        /// 仅登录校验
        /// </summary>
        CheckLogin,
        ///// <summary>
        ///// 全部校验
        ///// </summary>
        //CheckAll
    }
}